package scct.chenhui.dao;

import scct.chenhui.model.Brand;
import scct.chenhui.model.User;
import scct.chenhui.utils.DBUtils;

import java.rmi.Naming;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BrandDao {
        public boolean addBrand(String name,String company)
    {
//      String sql=  "insert into sm_brand  value (null,"jack","123","1","男","1357777")"
        String insertSql="insert into sm_brand  values (null,?,?)";

        int result;
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement pstmt = connection.prepareStatement(insertSql);
            pstmt.setString(1,name);
            pstmt.setString(2,company);
            result = pstmt.executeUpdate();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
      return  result==1;
    }

    public List<Brand> getAllBrand()
    {
        List<Brand> list = new ArrayList<>();
        try {
            Connection conn = DBUtils.getConnection();
            String sql = "select * from sm_brand";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String company = rs.getString("company");

                list.add(new Brand(id, name,company));

            }
            DBUtils.closeConnection(conn);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        return list;
    }
}
